package Utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Random;
import java.util.Scanner;

import main.SharedPref;
import Utils.Freebase.FB_ParsingUtils;
import Utils.UI.UIUpdater;

public class MiscUtils {
	private static String iniFileName = "settings.ini";

	// initialize current working directory
	private static void initCurrentDir() {
		SharedPref.currentDir = System.getProperty("user.dir");
	}

	// initialize file separator character according to the current OS
	private static void initFileSeperator() {
		String osName = System.getProperty("os.name");
		if (osName.contains("Windows")) {
			SharedPref.fileSeparator = "\\";
		} else if (osName.contains("Linux")) {
			SharedPref.fileSeparator = "/";
		}

	}

	/**
	 * Initialize settings for the application runtime
	 */
	private static void initSettings() {

		initCurrentDir();
		initFileSeperator();

		String SettingsfilePath = SharedPref.currentDir
				+ SharedPref.fileSeparator + iniFileName;
		String line = null;

		try {
			BufferedReader bi = new BufferedReader(new FileReader(SettingsfilePath),256);
			while ((line = bi.readLine()) != null){
 
				// break settings line into two halves separated by '='
				String[] parts = line.split("=");

				if (parts.length != 2) {
					continue;
				}

				// handle <setting,value>
				HandleSetting(parts[0].trim(), parts[1].trim());
			}
			
			bi.close();

		} catch (Exception e) {
			String errStr = "unexpected error while reading .ini file at + "
					+ SettingsfilePath + " ( " + e + " )";
			System.out.println(errStr);
			
			UIUpdater.showErrMsg(errStr);
			
		}
		
		System.out.println(SharedPref.Tv_DB_Folder);
		System.out.println(SharedPref.Film_DB_Folder);
		System.out.println(SharedPref.Measurement_Unit_Folder);
		System.out.println(SharedPref.forName);
		System.out.println(SharedPref.DBurl);
		System.out.println(SharedPref.password);
		System.out.println(SharedPref.dbName);
		System.out.println(SharedPref.schemaName);
		System.out.println(SharedPref.chainLengthForLongMod);
		System.out.println(SharedPref.timerTime);
		System.out.println(SharedPref.levelModLengths);
		System.out.println(SharedPref.DEBUG);
		
	}

	/**
	 * Update SharedPref according to the appropriate <setting,value>
	 */
	private static void HandleSetting(String setting, String value) {

		if (setting.equalsIgnoreCase("DB_Folder\\TV")) {
			SharedPref.Tv_DB_Folder = value;
		}

		if (setting.equalsIgnoreCase("DB_Folder\\FILM")) {
			SharedPref.Film_DB_Folder = value;
		}

		if (setting.equalsIgnoreCase("DB_Folder\\MEASUREMENT_UNIT")) {
			SharedPref.Measurement_Unit_Folder = value;
		}

		if (setting.equalsIgnoreCase("forName")) {
			SharedPref.forName = value;
		}

		if (setting.equalsIgnoreCase("DBurl")) {
			SharedPref.DBurl = value;
		}

		if (setting.equalsIgnoreCase("password")) {
			SharedPref.password = value;
		}

		if (setting.equalsIgnoreCase("dbName")) {
			SharedPref.dbName = value;
		}
		
		if (setting.equalsIgnoreCase("schemeName")) {
			SharedPref.schemaName = value;
		}

		if (setting.equalsIgnoreCase("chainLengthForLongMod")) {
			try {
				SharedPref.chainLengthForLongMod = Integer.parseInt(value);
			} catch (Exception e) {
				SharedPref.chainLengthForLongMod = 10;
			}
		}
		
		if(setting.equalsIgnoreCase("timerTime")){
			try{
				SharedPref.timerTime = Integer.parseInt(value);
			}catch(Exception e){
				SharedPref.timerTime = 20;
			}
		}

		if (setting.equalsIgnoreCase("levelModLengths")) {
			try {
				String[] lengths = value.split(",");
				SharedPref.levelModLengths = new int[lengths.length];
				for (int i = 0; i < lengths.length; i++) {
					SharedPref.levelModLengths[i] = Integer
							.parseInt(lengths[i]);
				}

			} catch (Exception e) {
				SharedPref.levelModLengths = new int[6];
				SharedPref.levelModLengths[0] = 2;
				SharedPref.levelModLengths[1] = 4;
				SharedPref.levelModLengths[2] = 8;
				SharedPref.levelModLengths[3] = 16;
				SharedPref.levelModLengths[4] = 16;
				SharedPref.levelModLengths[5] = 16;
			}
		}

		if (setting.equalsIgnoreCase("DEBUG")) {
			if (value.equalsIgnoreCase("true")) {
				SharedPref.DEBUG = true;
			} else {
				SharedPref.DEBUG = false;
			}
		}

	}

	public static void prepareProgram() {

		// initialize settings for sharedPref
		initSettings();

		// initialize freebase parsing values
		FB_ParsingUtils.init();
	}

	public static int GetRandomNumber(int low, int high) {
		Random randomGenerator = new Random();
		return randomGenerator.nextInt(high) + low;
	}

}
